package com.zkingsoft.actions.visitor;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tencent.protocol.simple_protocol.SimpleProtol;
import com.zkingsoft.actions.common.BaseController;
import com.zkingsoft.constance.Dictionary;
import com.zkingsoft.model.AdvertisementPosition;
import com.zkingsoft.model.Fy;
import com.zkingsoft.plugin.pojo.PaginationVO;
import com.zkingsoft.plugin.util.LogUtil;
import com.zkingsoft.plugin.util.WebUtil;
import com.zkingsoft.pojo.AjaxResult;
import com.zkingsoft.services.AdvertisementColumnService;
import com.zkingsoft.services.AdvertisementPositionService;
import com.zkingsoft.services.FyService;

/**
 * 
 * @Description: 不需要登录可以访问的action
 * @author:姜友瑶
 * @date 2016年8月31日
 */
@Controller
public class Home extends BaseController {

	@Resource
	private FyService fyService;
	@Resource
	private AdvertisementPositionService adPositionService;
	@Resource
	private AdvertisementColumnService advertisementColumnService;

	/**
	 * 
	 * @Description: 首页配置
	 * @author:姜友瑶
	 * @return 返回类型 String
	 * @date 2016年8月28日
	 */
	@RequestMapping(value = "/glylogin1234")
	public String redirect() {
		return "redirect:/common/redirect/login";
	}

	@RequestMapping(value = "/")
	public String index() {
		// 判断是否进行了自动登录
		if (WebUtil.isWeiXin() && WebUtil.getSession().getAttribute(Dictionary.AUTO_LOGINED) == null) {
			LogUtil.info(this.getClass(), "callback url= " + Dictionary.WXAUTODL_CALLBACK);
			WebUtil.getSession().setAttribute("callback", WebUtil.getCurrentPageUrl());
			return "redirect:" + SimpleProtol.getUserSq(Dictionary.WXAUTODL_CALLBACK, "snsapi_base",
					"" + System.currentTimeMillis());
		}

		queryIndexData();
		return "visitor/index";
	}

	@RequestMapping(value = "/MP_verify_vvC40J7YlYme02Sn.txt")
	public String weixinyanghznagfile() {
		queryIndexData();
		return "visitor/MP_verify_vvC40J7YlYme02Sn";
	}

	private void queryIndexData() {
		List<AdvertisementPosition> ads = adPositionService.findPointByCloumName(Dictionary.DJDZK);
		WebUtil.getRequest().setAttribute("djdzk", ads);
		List<AdvertisementPosition> syhdp = adPositionService.findPointByCloumName(Dictionary.SYHDP);
		WebUtil.getRequest().setAttribute("syhdp", syhdp);
		List<AdvertisementPosition> sygg2 = adPositionService.findPointByCloumName(Dictionary.SYGG2);
		WebUtil.getRequest().setAttribute("sygg2", sygg2);
		List<AdvertisementPosition> sygg = adPositionService.findPointByCloumName(Dictionary.SYGG);
		if (sygg.size() == 1) {
			WebUtil.getRequest().setAttribute("sygg", sygg.get(0));
		}
		Fy fy = new Fy();
		PaginationVO pageVo = new PaginationVO();
		fy.setGlPublishStatus("上架");
		fy.setYxzt("有效");
		fy.setGlCheckStatus("审核通过");
		List<Fy> fyList = fyService.findInPage(fy, pageVo);
		WebUtil.getRequest().setAttribute("newFy", fyList);
		fyService.findInPage(fy, pageVo);
	}

	@RequestMapping(value = "/indexFy")
	public @ResponseBody AjaxResult indexFy(PaginationVO pageVo) {
		Fy fy = new Fy();
		fy.setGlPublishStatus("上架");
		fy.setYxzt("有效");
		fy.setGlCheckStatus("审核通过");
		return new AjaxResult(AjaxResult.STATUS_OK, null, "", fyService.findInPage(fy, pageVo),
				fyService.findTotal(fy));
	}

	@RequestMapping(value = "/index")
	public String toindex() {
		// 判断是否进行了自动登录
		if (WebUtil.isWeiXin() && WebUtil.getSession().getAttribute(Dictionary.AUTO_LOGINED) == null) {
			LogUtil.info(this.getClass(), "callback url= " + Dictionary.WXAUTODL_CALLBACK);
			WebUtil.getSession().setAttribute("callback", WebUtil.getCurrentPageUrl());
			return "redirect:" + SimpleProtol.getUserSq(Dictionary.WXAUTODL_CALLBACK, "snsapi_base",
					"" + System.currentTimeMillis());
		}
		queryIndexData();
		return "visitor/index";
	}

}